package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.deal.MmochatDealLog;
import com.dragon.mmochat.model.deal.MmochatDealObjs;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

//交易记录
public class MmochatDealLogDao extends SimpleJdbcDaoSupport {
	private static final DealLogMapper dealLogMapper = new DealLogMapper();
	private static final RoleId1Mapper roleId1Mapper = new RoleId1Mapper();
	private static final RoleId2Mapper roleId2Mapper = new RoleId2Mapper();

	// 增加交易记录
	private static final String addNewDealLogSql = "insert into deal_log(role1_id, "
			+ "role1_imei, role1_deal_package, role2_id, role2_imei, role2_deal_package, date) "
			+ "values(?, ?, ?, ?, ?, ?, ?)";

	public void addNewDealLog(int role1_id, String role1_imei,
			String role1_deal_package, int role2_id, String role2_imei,
			String role2_deal_package) {
		getSimpleJdbcTemplate().update(addNewDealLogSql, role1_id, role1_imei,
				role1_deal_package, role2_id, role2_imei, role2_deal_package,
				new Timestamp(System.currentTimeMillis()));
	}

	// 查询指定玩家的交易记录
	private static final String queryRoleDealLogSql = "select * from deal_log"
			+ " where (role1_id = ? or role2_id = ?) and date >= ? and date < ? order by date desc";

	public List<MmochatDealLog> queryRoleDealLog(int roleId,
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(queryRoleDealLogSql,
				dealLogMapper, roleId, roleId, startDate, endDate);
	}

	// 查询指定交易记录
	private static final String queryRoleDealLogByIdSql = "select * from deal_log"
			+ " where id = ?";

	public MmochatDealLog queryRoleDealLogById(int id) {
		return getSimpleJdbcTemplate().queryForObject(queryRoleDealLogByIdSql,
				dealLogMapper, id);
	}

	// 删除2个月前的交易记录
	private static final String delDealLogByTimeSql = "delete from deal_log where date < ?";

	public void delDealLogByTime(Timestamp date) {
		getSimpleJdbcTemplate().update(delDealLogByTimeSql, date);
	}

	// 读取交易记录最大值
	private static final String getDelDealLogMaxIdSql = "select max(id) from deal_log";

	public int getDelDealLogMaxId() {
		return getSimpleJdbcTemplate().queryForInt(getDelDealLogMaxIdSql);
	}

	// 读取交易记录最小值
	private static final String getDelDealLogMinIdSql = "select min(id) from deal_log where date > ?";

	public int getDelDealLogMinId(Timestamp startDate) {
		return getSimpleJdbcTemplate().queryForInt(getDelDealLogMinIdSql,
				startDate);
	}

	// 读取有借条的roleId1
	private static final String queryBorrowedRole1DealLogSql = "select role1_id from deal_log "
			+ "where position('借条' in role1_deal_package) > 0 and id >= ? and id < ?";

	public List<Integer> queryBorrowedRole1DealLog(int s, int e) {
		return getSimpleJdbcTemplate().query(queryBorrowedRole1DealLogSql,
				roleId1Mapper, s, e);
	}

	// 读取有借条的roleId2
	private static final String queryBorrowedRole2DealLogSql = "select role2_id from deal_log "
			+ "where position('借条' in role2_deal_package) > 0 and id >= ? and id < ?";

	public List<Integer> queryBorrowedRole2DealLog(int s, int e) {
		return getSimpleJdbcTemplate().query(queryBorrowedRole2DealLogSql,
				roleId2Mapper, s, e);
	}

	static class RoleId1Mapper implements ParameterizedRowMapper<Integer> {
		@Override
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			return rs.getInt("role1_id");
		}
	}

	static class RoleId2Mapper implements ParameterizedRowMapper<Integer> {
		@Override
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			return rs.getInt("role2_id");
		}
	}

	static class DealLogMapper implements
			ParameterizedRowMapper<MmochatDealLog> {
		@Override
		public MmochatDealLog mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatDealLog deal = new MmochatDealLog();
			deal.setId(rs.getLong("id"));
			deal.setTime(rs.getTimestamp("date"));
			deal.setRoleId1(rs.getInt("role1_id"));
			deal.setRoleId2(rs.getInt("role2_id"));
			deal.setRole1Imei(rs.getString("role1_imei"));
			deal.setRole2Imei(rs.getString("role2_imei"));

			Gson gson = new Gson();
			String str = rs.getString("role1_deal_package");
			if (str != null) {
				MmochatDealObjs role1_deal_package = gson.fromJson(str,
						new TypeToken<MmochatDealObjs>() {
						}.getType());
				if (role1_deal_package != null) {
					deal.setRole1DealObjs(role1_deal_package);
				}
			}

			str = rs.getString("role2_deal_package");
			if (str != null) {
				MmochatDealObjs role2_deal_package = gson.fromJson(str,
						new TypeToken<MmochatDealObjs>() {
						}.getType());
				if (role2_deal_package != null) {
					deal.setRole2DealObjs(role2_deal_package);
				}
			}
			return deal;
		}
	}
}
